/**
 * 
 */
package com.depot.org.user.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.LoginUser;
import com.depot.DepotConstants;
import com.depot.domains.Users;
import com.depot.org.user.dao.UsersDao;
import com.opensymphony.xwork2.ModelDriven;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;

public class UsersAction extends BaseActionSupport implements ModelDriven<Users>
{
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public Users domain = new Users();

	/**
	 * 
	 */
	public HashMap<String, String> users = new HashMap<String, String>();

	public List<HashMap<String, String>> orgList = new ArrayList<HashMap<String, String>>();

	public Map<String, String> rolemap = new HashMap<String, String>();

	public String isorgadminuser = "";
	
	public String issupadmin = "";

	/**
	 * @param env
	 * @return
	 * @throws Exception
	 */
	public String execute(Environment env) throws Exception
	{
		String rolecode = env.getLoginUser(LoginUser.class).getUserRoleCode();
		if (DepotConstants.ADMIN_CODE.equals(rolecode))
		{
			issupadmin = "supadmin";
			this.orgList = UsersDao.getorg(env);
		}
		this.rolemap = UsersDao.getrolemap(env);

		this.sysPageList = UsersDao.select(env, domain, this);
		return "user_list";
	}

	/**
	 * 
	 * @param env
	 * @return
	 * @throws Exception
	 */
	public String doNew(Environment env) throws Exception
	{
		this.orgList = UsersDao.getorg(env);
		return "user_edit";
	}

	/**
	 * @param env
	 * @return
	 * @throws Exception
	 */
	public String doShow(Environment env) throws Exception
	{
		String rolecode = env.getLoginUser(LoginUser.class).getUserRoleCode();
		if (DepotConstants.ADMIN_CODE.equals(rolecode))
		{
			issupadmin = "supadmin";
		}
		this.users = UsersDao.doShow(env, domain);
		if (UsersDao.isOrgAdmin(env, users.get("ID"), users.get("ORG_ID")))
			this.isorgadminuser = "isorgadminuser";
		this.orgList = UsersDao.getorg(env);
		return "user_edit";
	}

	/**
	 * @param env
	 * @return
	 * @throws Exception
	 */
	public String doSave(Environment env) throws Exception
	{

		String result = UsersDao.doSave(env, domain);
		if (result.equals("1"))
		{
			this.setMessage("操作处理成功！", env, true);
		}
		else if (result.equals("0"))
		{
			this.setMessage("操作处理失败，请重试！", env, false);
		}
		else if (result.equals("2"))
		{
			this.setMessage("号码已被使用", env, false);
		}
		return this.execute(env);
	}

	public String doEdit(Environment env) throws Exception
	{
		String result = UsersDao.doSave(env, domain);
		if (result.equals("1"))
		{
			this.setMessage("操作处理成功！", env, true);
		}
		else if (result.equals("0"))
		{
			this.setMessage("操作处理失败，请重试！", env, false);
		}
		return this.execute(env);
	}

	public Users getModel()
	{
		return this.domain;
	}

}